package com.lv.service.utils

import android.content.Context
import android.os.Bundle
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.LogUtils
import com.lv.common.YgxManager
import com.lv.common.im.push.OfflineMessageDispatcher
import com.lv.common.utils.ARoutUtils
import com.lv.common.utils.YgxUtilsKt
import com.lv.service.repo.UserDbHelper
import com.lv.service.repo.UserInfo
import com.tencent.imsdk.v2.V2TIMConversation
import com.tencent.qcloud.tuicore.TUIConstants
import com.tencent.qcloud.tuicore.TUICore
import com.tencent.qcloud.tuicore.TUILogin
import com.tencent.qcloud.tuicore.interfaces.TUICallback
import com.tencent.qcloud.tuikit.tuichat.bean.message.GoodsChatMessageBean

/**
 * 作者： ygx
 * 创建日期：2023/4/11 2:02 PM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 * <p>
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 * 描述：
 */

class UserUtils {
    companion object {
        fun getUser(context: Context): UserInfo? {
            return UserDbHelper.getUserInfo(context)
        }

        /**
         * 判断是否登录
         *
         * @return
         */
        fun isLogin(context: Context): Boolean {
            return getUser(context) != null
        }

        /**
         * 清除全部用户登录数据并退出云信
         */
        fun clearUser(context: Context) {
//        NIMClient.getService(AuthService.class).logout();
            UserDbHelper.deleteUserInfo(context)
//            val logOutEvent = LogOutEvent()
//            logOutEvent.setLogin(false)
//            EventBus.getDefault().post(logOutEvent)
        }

        /**
         * 判断是不是公司
         */
//        val isCompany: Boolean
//            get() {
//                if (user != null) {
//                    if (user.getCompany() != null) {
//                        if (user.getUserLevel() === 1 || user.getRole().contains("b")) {
//                            return true
//                        }
//                    }
//                }
//                return false
//            }

        /**
         * 判断是不是公司
         */
        fun phone(context: Context): String {

            return getUser(context)?.phone ?: ""
        }


        /**
         * 退出当前用户登录
         */
//        fun exitLogin(): Boolean {
////        NIMClient.getService(AuthService.class).logout();
//            val helperForUser = DataHelperForUser()
//            val user: User = helperForUser.getUser() ?: return false
//            val phone = phone
//            return helperForUser.deleteUser(phone)
//        }

        /**
         * 跳转聊天页面
         */
        fun showChatActivity(accid: String?, name: String?, attachment: GoodsChatMessageBean?) {
            val param = Bundle()
            param.putInt(TUIConstants.TUIChat.CHAT_TYPE, V2TIMConversation.V2TIM_C2C)
            param.putString(TUIConstants.TUIChat.CHAT_ID, accid)
            param.putString(TUIConstants.TUIChat.CHAT_NAME, name)
            param.putSerializable("ShopInfoChatBean", attachment)
            //                        if (conversationInfo.getDraft() != null) {
//                            param.putString(TUIConstants.TUIChat.DRAFT_TEXT, conversationInfo.getDraft().getDraftText());
//                            param.putLong(TUIConstants.TUIChat.DRAFT_TIME, conversationInfo.getDraft().getDraftTime());
//                        }
            TUICore.startActivity(TUIConstants.TUIChat.C2C_CHAT_ACTIVITY_NAME, param)
        }

        /**
         * 跳转聊天页面
         */
        fun showChatActivity(accid: String?, name: String?) {
            val param = Bundle()
            param.putInt(TUIConstants.TUIChat.CHAT_TYPE, V2TIMConversation.V2TIM_C2C)
            param.putString(TUIConstants.TUIChat.CHAT_ID, accid)
            param.putString(TUIConstants.TUIChat.CHAT_NAME, name)
//            param.putSerializable("ShopInfoChatBean", attachment)
            //                        if (conversationInfo.getDraft() != null) {
//                            param.putString(TUIConstants.TUIChat.DRAFT_TEXT, conversationInfo.getDraft().getDraftText());
//                            param.putLong(TUIConstants.TUIChat.DRAFT_TIME, conversationInfo.getDraft().getDraftTime());
//                        }
            TUICore.startActivity(TUIConstants.TUIChat.C2C_CHAT_ACTIVITY_NAME, param)
        }

        fun loginOut() {
            LogUtils.e("退出退出退出")
            try {
                UserDbHelper.deleteUserInfo(YgxManager.context!!)
                OfflineMessageDispatcher.updateBadge(YgxManager.context, 0)
                //清除Token
                YgxUtilsKt.setAppToken("", "", "")

                TUILogin.logout(object : TUICallback() {
                    override fun onSuccess() {
                        //                            showToast("IM退出登录成功")
                    }

                    override fun onError(errorCode: Int, errorMessage: String?) {
                        //                            showToast("IM退出登录失败")
                    }
                })
                ARouter.getInstance().build(ARoutUtils.LoginActivityURL).navigation()
            } catch (e: Exception) {
            }
        }
    }

}